﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using TimeTracker.Core.Domain.Entities;
using TimeTracker.Web.Admin.Interface;
using TimeTracker.Web.Admin.Presenter;

namespace TimeTracker.Web.Admin
{
    public partial class Radnici : System.Web.UI.Page, IRadniciView
    {
        private RadniciPresenter _presenter;
        private DropDownList _ddlOrgJed = new DropDownList();

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new RadniciPresenter();
            _presenter.Init(this);

            if (!IsPostBack)
            {
                _presenter.Init(this, "Radnici");
            }
        }

        public void PokaziRadnike(IList<Radnik> radnici)
        {
            gvRadnici.DataSource = radnici;
            gvRadnici.DataBind();
        }

        public void PokaziOrganizacioneSeme(IList<OrganizacionaSema> seme)
        {
            _ddlOrgJed.DataValueField = "OrganizacionaSemaId";
            _ddlOrgJed.DataTextField = "ImeOrganizacioneSeme";
            _ddlOrgJed.DataSource = seme;
            _ddlOrgJed.DataBind();
        }

        public void PokaziOrganizacioneSeme(IList<OrganizacionaSema> seme, int semaId)
        {
            _ddlOrgJed.DataValueField = "OrganizacionaSemaId";
            _ddlOrgJed.DataTextField = "ImeOrganizacioneSeme";
            _ddlOrgJed.DataSource = seme;
            _ddlOrgJed.DataBind();

            _ddlOrgJed.Items.FindByValue(semaId.ToString()).Selected = true;            
        }

        protected void GridViewRadnici_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Dodaj"))
            {
                var ime = (TextBox)gvRadnici.FooterRow.FindControl("tbNovoIme");
                var prezime = (TextBox)gvRadnici.FooterRow.FindControl("tbNovoPrezime");
                var email = (TextBox)gvRadnici.FooterRow.FindControl("tbNoviEmail");
                
                _ddlOrgJed = (DropDownList) gvRadnici.FooterRow.FindControl("ddlNovaOrganizacionaJedinica");
                var orgSemaId = Convert.ToInt32(_ddlOrgJed.SelectedValue);

                _presenter.DodajRadnika(ime.Text, prezime.Text, email.Text, orgSemaId);
                
                gvRadnici.EditIndex = -1;
                _presenter.Init(this, "Radnici");
            }
            
            if (!e.CommandName.Equals("Otkazi")) return;

            gvRadnici.EditIndex = -1;
            _presenter.Init(this, "Radnici");
        }

        protected void GridViewRadnici_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var radnikId = Convert.ToInt32(gvRadnici.DataKeys[e.RowIndex].Values[0].ToString());
            
            _presenter.ObrisiRadnika(radnikId);
            _presenter.Init(this, "Radnici");
        }

        protected void GridViewRadnici_OnRowEditing(object sender, GridViewEditEventArgs e)
        {
            gvRadnici.EditIndex = e.NewEditIndex;
            _presenter.Init(this, "Radnici");
        }

        protected void GridViewRadnici_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvRadnici.EditIndex = -1;
            _presenter.Init(this, "Radnici");
        }

        protected void GridViewRadnici_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var radnikId = (int)gvRadnici.DataKeys[e.RowIndex].Value;

            var ime = (TextBox)gvRadnici.Rows[e.RowIndex].FindControl("tbIme");
            var prezime = (TextBox)gvRadnici.Rows[e.RowIndex].FindControl("tbPrezime");
            var email = (TextBox)gvRadnici.Rows[e.RowIndex].FindControl("tbEmail");

            _ddlOrgJed = (DropDownList)gvRadnici.Rows[e.RowIndex].FindControl("ddlOrganizacionaJedinica");
            var orgSemaId = Convert.ToInt32(_ddlOrgJed.SelectedValue);

            _presenter.AzurirajRadnika(radnikId, ime.Text, prezime.Text, email.Text, orgSemaId);

            gvRadnici.EditIndex = -1;
        }

        protected void GridViewRadnici_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType == DataControlRowType.DataRow)
            {
                
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                _ddlOrgJed = (DropDownList)e.Row.FindControl("ddlNovaOrganizacionaJedinica");
                _presenter.Init(this, "OrganizacioneSeme");
            }
            if(e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                var radnikId = (int)gvRadnici.DataKeys[e.Row.RowIndex].Values[0];

                _ddlOrgJed = (DropDownList)e.Row.FindControl("ddlOrganizacionaJedinica");
                _presenter.Init(this, "OrganizacioneSeme", radnikId);
            }
        }
    }
}
